package com.examsystem.dao;


import com.examsystem.pojo.Paper;
import com.examsystem.pojo.vo.PaperVo;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author fire'ge
 */
@Mapper
public interface PaperMapper {

    /*
    学生端
     */

    /**添加一个Paper
     * @param paper 传入paper类
     * @return int
     */
    int addPaper(Paper paper);
    /**根据id删除一个Paper
     * @param id 传入id
     * @return int
     */
    int deletePaperById(int id);

    /**更新Paper
     * @param paper 传入paper类
     * @return int
     */
    int updatePaper(Paper paper);
    /**根据id查询,返回一个Paper
     * @param id 传入id
     * @return int
     */
    Paper queryPaperById(int id);

    /**查询全部Paper,返回list集合
     * @return List<Paper>
     */
    List<Paper> queryAllPaper();


    /**通过userId查询全部Paper,返回list集合
     * @param userId 用户的id
     * @return List<Paper>
     */
    List<Paper> queryPaperByUserId(@Param("userId") int userId);





    /*
    教师端
     */

    /**
     * 创建新试卷
     * @param paperTeacher 教师id
     * @param dateTime  时间
     * @param paperName  试卷名
     * @param paperSubject  试卷科目
     * @return 【1：成功 0：失败】
     */
    @Insert("insert into paper values (0,#{paperTeacher},#{dateTime},#{paperName},#{paperSubject},100)")
     int newPaper(@Param("paperTeacher") int paperTeacher, @Param("dateTime") String dateTime,@Param("paperName") String paperName,@Param("paperSubject") String paperSubject);

    /**
     * 获取教师科目
     * @param teacherId  教师id
     * @return 科目
     */
    @Select("select subjectName from teacherSubject where teacherId = #{teacherId}")
    String getSubject(int teacherId);

    /**
     * 试卷与试题的关系
     * @param paperId  试卷id
     * @param questionId  试题id
     * @return  【0：失败  1：成功】
     */
    @Insert("insert into paper_question values (0,#{questionId},#{paperId},0)")
    int paperQuestion(@Param("paperId") int paperId,@Param("questionId") int questionId);

    /**
     * 获取试卷的一些信息
     * @param paperId  试卷id
     * @return  paper对象
     */
    @Select("select *from paper where paper_id = #{paperId}")
    @Results({
            @Result(column = "paper_id" ,property = "paperId"),
            @Result(column = "paper_teacher",property = "paperTeacher"),
            @Result(column = "datetime",property = "dateTime"),
            @Result(column = "paper_name",property = "paperName"),
            @Result(column = "paper_subject",property = "paperSubject")
    })
    Paper getPaper(int paperId);


    /**
     * 查询整张试卷试题
     * @param paperId  试卷id
     * @return  paperVo对象
     */
    @Select("select paperId,paperName,paperSubject,datetime from paperV where paperId=#{paperId}")
    @Results({
            @Result(column = "paperId",property = "paper.paperId"),
            @Result(
                    property = "questionList",
                    column = "paperId",
                    javaType = List.class,
                    many = @Many(select = "com.examsystem.dao.QuestionMapper.findQuestion")
            ),
            @Result(column = "paperName",property = "paper.paperName"),
            @Result(column = "paperSubject",property = "paper.paperSubject"),
            @Result(column = "datetime",property = "paper.dateTime")
    })
    PaperVo paperQuestions(int paperId);

    /**
     * 根据用户点击进去的id进行查询循环遍历出整张试卷的所有试题
     * @param paperId 试卷id
     * @return paper对象
     */
    @Select("select *from exam.paper where paper_id= #{paperId}")
    @Results(
            {
                    @Result(column = "paper_id",property = "paperId"),
                    @Result(column = "paper_name",property = "paperName"),
                    @Result(column = "paper_teacher",property = "paperTeacher"),
                    @Result(column = "datetime",property = "paperDateTime"),
                    @Result(
                            property = "questionList",
                            column = "paper_id",
                            javaType = List.class,
                            many = @Many(select = "com.example.exam.mapper.PaperMapper.findQuestionOne")
                    )

            })
    List<Paper> selectAllPaper(int paperId);


    /**
     * 自动交卷
     * @param paperId  试卷id
     * @param studentId  学生id
     * @return 【1：自动交卷成功（主要以当作考生已经考过这次考试为主，即一旦开始考试就会被视为参加考试，会自动交卷）
     *          0：自动交卷失败】
     */
    @Update("update exam.user_paper set up__sta = 1  where up_paper = #{paperId} and up_user = #{studentId} ")
    int putInPaper(int paperId,int studentId);

    /**
     * 获取开始考试时间跟考试时长
     * @param paperId 试卷id
     * @param classId  班级id
     * @return 【K,V】:【开始考试时间，考试时长（单位：时间）】
     */
    @Select("select pc.pc_time from exam.paper_class pc,exam.user_paper up " +
            "where pc.pc_paper=up.up_paper and pc.pc_class = #{classId} and pc_paper =#{paperId}")
    String getStartTime(int paperId,int classId);

    /**
     * 获取该次考试时长
     * @param paperId  试卷id
     * @param classId  班级id
     * @return  考试时长
     */
    @Select("select pc.pc_examTime from exam.paper_class pc,exam.user_paper up " +
            "where pc.pc_paper=up.up_paper and pc.pc_class = #{classId} and pc_paper =#{paperId}")
    Integer getTime(int paperId,int classId);

    /**
     * 获取考生此次考试的试卷的状态【1：已经提交过  0：还没提交】
     * @param studentId  学生id
     * @param paperId  试卷id
     * @return 【状态的标志性数字：1或0】
     */
    @Select("select up__sta from exam.user_paper where up_paper= #{paperId} and up_user = #{studentId}")
    int getPaperStatus(int studentId,int paperId);

    /**
     * 获取所有的试卷
     * @return  list集合的paper对象
     */
    @Select("select *from paper")
    @Results({
            @Result(column = "paper_id",property = "paperId"),
            @Result(column = "paper_teacher",property = "paperTeacher"),
            @Result(column = "datetime",property = "dateTime"),
            @Result(column = "paper_name",property = "paperName"),
            @Result(column = "paper_subject",property = "paperSubject"),
            @Result(column = "paper_status",property = "paperStatus")
    })
    List<Paper> showAllPaper();

}
